package  
{
	import fl.controls.BaseButton;
	import fl.controls.Button;
	import fl.controls.CheckBox;
	import fl.controls.RadioButton;
	import fl.controls.TextArea;
	import fl.controls.TextInput;
	import fl.core.UIComponent;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	
	/**
	 * ...
	 * @author erik
	 */
	public class MainView extends Sprite
	{
		public var inputFileRadio:RadioButton;
		public var inputBrowseBtn:Button;
		public var inputBrowseTf:TextField;
		public var inputDirectRadio:RadioButton;
		public var inputTa:TextArea;
		public var outputBrowseTf:TextField;
		public var convertBtn:Button;
		public var saveBtn:Button;
		public var saveasBtn:Button;
		public var statusTf:TextField;
		public var openCb:CheckBox;
		public var excludeTablesInput:TextInput;
		
		private var components:Array;
		
		public static const INPUT_BROWSE:String = "inputbrowse";
		public static const CONVERT:String = "convert";
		public static const SAVE:String = "save";
		public static const SAVE_AS:String = "save_as";
		
		
		public function MainView() 
		{
			components = [inputFileRadio, inputBrowseBtn, inputDirectRadio, convertBtn, saveBtn, saveasBtn, openCb];
			
			inputBrowseBtn.addEventListener(MouseEvent.MOUSE_DOWN, onInputBrowseFileBtnClick);

			
			convertBtn.addEventListener(MouseEvent.MOUSE_DOWN, onConvert);
			saveBtn.addEventListener(MouseEvent.MOUSE_DOWN, onSave);
			saveasBtn.addEventListener(MouseEvent.MOUSE_DOWN, onSave);
			
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}
		
		private function onAddedToStage(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKD);
			
			
		}
		

		
		private function onKD(e:KeyboardEvent):void 
		{
			switch (e.keyCode)
			{
				case Keyboard.F5 :
					dispatchEvent(new Event(CONVERT));
					break;
			}
		}

		private function onInputBrowseFileBtnClick(e:MouseEvent):void 
		{
			setStatus();
			dispatchEvent(new Event(INPUT_BROWSE));
		}
				
				
		private function onConvert(e:MouseEvent):void 
		{
			trace("MainView::onConvert");
			dispatchEvent(new Event(CONVERT));
		}
		
		private function onSave(e:MouseEvent):void
		{
			trace("MainView::onSave");
			dispatchEvent(new Event((e.target == saveasBtn) ? SAVE_AS : SAVE));
		}
		
		
		
		
		public function setStatus(m:String ="", err:Boolean = false, lock:Boolean = false, lockId:String = null):void
		{
			/*if (statusTfLock == lockId)
			{
				return;
			}
			else statusTfLock = "none";
			
			if (lock)
			{
				statusTfLock = lockId;
			}*/
			
			var fmt:TextFormat = statusTf.getTextFormat();
			fmt.color = (err) ? 0xFF0000 : 0x0;
			statusTf.defaultTextFormat = fmt;
			var d:Date = new Date();
			
			statusTf.text = (m=="") ? "" : "("+d.toLocaleTimeString()+") " + m;
		}
		
		
		public function get openWhenConvert():Boolean
		{
			return openCb.selected;
		}
		
		public function set inputFile(f:String):void
		{
			inputBrowseTf.text = f;
			inputFileRadio.selected = true;
		}		
		
		public function set outputFile(f:String):void
		{
			outputBrowseTf.text = f;
		}
		
		public function disable():void
		{
			for each (var item:BaseButton in components) 
			{
				item.enabled = false;
			}
		}
		
		public function enable():void
		{
			for each (var item:BaseButton in components) 
			{
				item.enabled = true;
			}
		}		
	}

}